<?php


require_once(DIR_LIB."VsCore".DIR_SEP."vsauth_data.php");

require_once(DIR_LIB."VsCore".DIR_SEP."vssession.php");
require_once(DIR_LIB."VsCore".DIR_SEP."db.php");

class VsAuth {

	public static $_Instance = NULL;

	private static $_sqlTable = 'admin_logins';
	
	private $_isAuth = FALSE;

	private $_authData = NULL;
	
	static function Instance() {
		if(!self::$_Instance)
			self::$_Instance = new VsAuth();

		return self::$_Instance;
	}
	//=================================================================
	
	function isAuth() { return $this->_isAuth; }
	function userId() { return $this->_authData->userId(); }
	function id() { return $this->userId(); }
	function userName() { return $this->_authData->userName(); }
	function name() { return $this->userName(); }
	function level() { return $this->_authData ? $this->_authData->level() : 0; } // 
	function lastLogin() {  }
	//================================================================
	
	function logout() {
		$_vsSession = VsSession::Instance();
		
		$_vsSession->remove('VsAuthSqlTable');
		$_vsSession->remove('VsAuthUserId');
		$_vsSession->remove('VsAuthLevel');
		$_vsSession->remove('VsAuthUser');
		$this->_isAuth = FALSE;
	}
	//================================================================
	
	function login($user, $password) {
		return $this->setAuth($user, $password);
	}
	//================================================================
	
	function setAuth($user, $password, $sqlTable='admin_logins') {
		$_vsSession = VsSession::Instance();
		$authData = VsAuthData::makeUserByName($user, $sqlTable);
		
		if($authData->isValid()) {
			if($authData->confirmPassword($password)) {
				$this->_authData = $authData;
				$this->setAuthSession($user);
			
				$_vsSession->assign('VsAuthSqlTable', $sqlTable);
				$_vsSession->assign('VsAuthUserId', $authData->userId());
				$_vsSession->assign('VsAuthUser', $authData->userName());
				$_vsSession->assign('VsAuthLevel', $authData->userLevel());

				$this->_isAuth = TRUE;
			}
			else
				return 1;	// Password Invalid
		}
		else
			return 2;	// User Not Exist
	}
	//================================================================
	
	private function __construct() {
		$_vsSession = VsSession::Instance();
		
		if($_vsSession->isExists('VsAuthLevel') && intval($_vsSession->value('VsAuthLevel'))) {
			$this->_authData = VsAuthData::makeUserByName($_vsSession->value('VsAuthUser'), $_vsSession->value('VsAuthSqlTable'));

			if(!$this->checkSession($_vsSession->value('VsAuthUser'))) {
				$this->_authData = VsAuthData::Anonymous();

				$_vsSession->remove('VsAuthSqlTable');
				$_vsSession->remove('VsAuthUserId');
				$_vsSession->remove('VsAuthLevel');
				$_vsSession->remove('VsAuthUser');

				$this->_isAuth = FALSE;
			}
			else {
				$this->_isAuth = TRUE;
				$this->_authData->setLastLogin();
			}
		}
    }
	//======================================================================================

	private function setAuthSession($vsAuthUser) {
		$this->_authData->setAuthSession($this);

		// Old Code
		/*
		$db = VsDatabase::Instance();
		
		$_vsSession = VsSession::Instance();
		
		$q = "UPDATE `users` SET `session_id`='".$_vsSession->id()."' WHERE `username`='".$vsAuthUser."'";
		//echo $q;
		$res = $db->query($q);
		*/
	}
	//======================================================================================

	private function checkSession($vsAuthUser) {
		return $this->_authData->confirmAuthSession($this);

		// Old Code
		/*
		$db = VsDatabase::Instance();
		
		$_vsSession = VsSession::Instance();

		$q = "SELECT `session_id` FROM `users` WHERE username='".$vsAuthUser."'";
		$res = $db->query($q);

		if(mysql_num_rows($res)<1)
			return FALSE;
		else {
			$row = mysql_fetch_object($res);
		
			return ($row->session_id==$_vsSession->id());
		}
		*/
	}
	//=====================================================================================
	
	/*
	private function userData($userName) {
		$db = VsDatabase::Instance();

		$q = "SELECT * FROM `users` WHERE username='".$userName."'";
		//echo $q;
		$res = $db->query($q);

		if(mysql_num_rows($res)<1)
			return FALSE;
		else {
			return mysql_fetch_object($res);
		}
	}
	//==================================================================================
	*/


};

?>
